/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.views;

import com.insurance.cis.controllers.EmployeeUpdateController;
import com.insurance.cis.daos.GroupDao;
import com.insurance.cis.models.Employee;
import com.insurance.cis.models.Gender;
import com.insurance.cis.models.Group;
import com.insurance.cis.utils.CisConstants;
import com.insurance.cis.utils.Utils;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

/**
 *
 * @author TruongLQ
 */
public class EmployeeJDialogUpdateView extends javax.swing.JDialog {

    MainJFrameView mainJFrame;
    Employee employee;
    GroupDao groupDao =new GroupDao();
    EmployeeUpdateController controller = new EmployeeUpdateController(this);
    /**
     * Creates new form EmployeeJDialogCreationView
     */
    public EmployeeJDialogUpdateView(java.awt.Frame parent, boolean modal, 
            Employee employee) {
        super(parent, modal);
        initComponents();
        mainJFrame = (MainJFrameView) parent;
        this.employee = employee;
        loadGroups();
        loadExistingEmployee();
        this.setResizable(false);
        this.setLocationRelativeTo(getRootPane());
    }
    
    private void loadGroups() {
        Vector<Group> groups = groupDao.getAllGroups();
        groups.add(0, new Group(null, "Select a group"));
        cbxGroups.setModel(new DefaultComboBoxModel(groups));
    }
    private void loadExistingEmployee() {
        if (employee != null) {
            txtEmpID.setText(employee.getCode());
            txtUserName.setText(employee.getUserName());
            txtPwd.setText(employee.getPassword());
            txtConfimPwd.setText(employee.getPassword());
            txtFullName.setText(employee.getName());
            if (employee.getDob()!=null) {
                txtBirthday.setText(Utils.convertDateToString(employee.getDob(), CisConstants.DATE_DD_MM_YYYY));
            }
            cbxGroups.setSelectedItem(employee.getGroup());
            setGender();
        }
    }
    
    private void setGender() {
        if (employee != null) {
            if (Gender.MALE.getDesc().equals(employee.getGender())) {
                rbtnMale.setSelected(true);
            } else if (Gender.FEMALE.getDesc().equals(employee.getGender())) {
                rbtnFemale.setSelected(true);
            }
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        rbtnGenderGroup = new javax.swing.ButtonGroup();
        txtEmpID = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtPwd = new javax.swing.JPasswordField();
        txtConfimPwd = new javax.swing.JPasswordField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtFullName = new javax.swing.JTextField();
        rbtnMale = new javax.swing.JRadioButton();
        rbtnFemale = new javax.swing.JRadioButton();
        txtBirthday = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        cbxGroups = new javax.swing.JComboBox();
        txtUserName = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        btnSave = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Update an existing employee");

        txtEmpID.setEditable(false);

        jLabel1.setText("Employee ID");

        jLabel4.setText("Group");

        jLabel2.setText("User Name");

        jLabel5.setText("Confirm Password");

        jLabel6.setText("Full Name");

        jLabel7.setText("Gender");

        jLabel8.setText("Birthday");

        rbtnGenderGroup.add(rbtnMale);
        rbtnMale.setText("Male");

        rbtnGenderGroup.add(rbtnFemale);
        rbtnFemale.setText("Female");

        jLabel3.setText("Password");

        txtUserName.setEditable(false);

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel9.setText("Update an existing employee");

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btnSave)
                            .addGap(18, 18, 18)
                            .addComponent(btnReset)
                            .addGap(18, 18, 18)
                            .addComponent(btnClose, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel5)
                                .addComponent(jLabel3)
                                .addComponent(jLabel2))
                            .addGap(32, 32, 32)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtConfimPwd, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtPwd, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtEmpID, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(39, 39, 39)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel6)
                                .addComponent(jLabel7)
                                .addComponent(jLabel8)
                                .addComponent(jLabel4))
                            .addGap(37, 37, 37)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtBirthday)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(rbtnMale)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(rbtnFemale))
                                .addComponent(txtFullName)
                                .addComponent(cbxGroups, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(55, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel3, jLabel5});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel4, jLabel6, jLabel7, jLabel8});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnClose, btnReset, btnSave});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel9)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtEmpID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(rbtnMale)
                    .addComponent(rbtnFemale))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtPwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(txtBirthday, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtConfimPwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(cbxGroups, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSave)
                    .addComponent(btnReset)
                    .addComponent(btnClose))
                .addContainerGap(37, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        Employee employeeUpdate = controller.validateEmployeeInfos();
        if (employeeUpdate == null) {
            return;
        }
        showSavingConfirmation(employeeUpdate);
    }//GEN-LAST:event_btnSaveActionPerformed

    private void showSavingConfirmation(Employee employee) {
        EmployeeJDialogConfirmationView dialog = 
                    new EmployeeJDialogConfirmationView(this, true, employee);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    }
    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCloseActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        if (employee!=null) {
            if (employee.getDob()!=null) {
                txtBirthday.setText(Utils.convertDateToString(
                        employee.getDob(), CisConstants.DATE_DD_MM_YYYY));
            } else {
                txtBirthday.setText("");
            }
            txtConfimPwd.setText(employee.getPassword());
            txtEmpID.setText(employee.getCode());
            txtFullName.setText(employee.getName());
            txtPwd.setText(employee.getPassword());
            txtUserName.setText(employee.getUserName());
            if (Gender.MALE.getDesc().equals(employee.getGender())){
                rbtnMale.setSelected(true);
            } else if (Gender.FEMALE.getDesc().equals(employee.getGender())) {
                rbtnFemale.setSelected(true);
            }
            cbxGroups.setSelectedItem(employee.getGroup());        
        }
        
    }//GEN-LAST:event_btnResetActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cbxGroups;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JRadioButton rbtnFemale;
    private javax.swing.ButtonGroup rbtnGenderGroup;
    private javax.swing.JRadioButton rbtnMale;
    private javax.swing.JTextField txtBirthday;
    private javax.swing.JPasswordField txtConfimPwd;
    private javax.swing.JTextField txtEmpID;
    private javax.swing.JTextField txtFullName;
    private javax.swing.JPasswordField txtPwd;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables

    public JComboBox getCbxGroups() {
        return cbxGroups;
    }

    public JRadioButton getRbtnFemale() {
        return rbtnFemale;
    }

    public JRadioButton getRbtnMale() {
        return rbtnMale;
    }

    public JTextField getTxtBirthday() {
        return txtBirthday;
    }

    public JPasswordField getTxtConfimPwd() {
        return txtConfimPwd;
    }

    public JTextField getTxtEmpID() {
        return txtEmpID;
    }

    public JTextField getTxtFullName() {
        return txtFullName;
    }

    public JPasswordField getTxtPwd() {
        return txtPwd;
    }

    public JTextField getTxtUserName() {
        return txtUserName;
    }
    
}
